package ua.kiev.tosik.price.modules.clients.gui.files.preview;

import org.jdesktop.swingx.JXCollapsiblePane;
import ua.kiev.tosik.price.modules.clients.conf.FilterConfig;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;
import ua.kiev.tosik.price.modules.clients.models.FilterAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class PricesFilesFilterPanel extends JXCollapsiblePane {

	private PricesFilesPreviewPanel previewPanel;
	private ModuleConfig config;
	private JToolBar toolBar;
	private JButton saveButton;

	private FilterConfig filterConfig;

	private Map<String, JButton> buttonMap;

	public PricesFilesFilterPanel(PricesFilesPreviewPanel previewPanel) {
		// TODO: Show combo with saved column templates
		this.previewPanel = previewPanel;
		config = ModuleConfig.getInstance();
		filterConfig = FilterConfig.getInstance();
		buttonMap = new HashMap<String, JButton>();
		setDirection(Direction.UP);
		setLayout( new BorderLayout() );

		toolBar = new JToolBar();
		toolBar.setFloatable(false);

		initButtons();

		toolBar.addSeparator();
		toolBar.add(saveButton);
		toolBar.addSeparator();

		add(toolBar, BorderLayout.NORTH);
	}

	public void setEnableButton(boolean enable) {
		for ( JButton button: buttonMap.values() ) {
			button.setEnabled(enable);
		}
		saveButton.setEnabled(enable);
	}

	private void initButtons() {
		for ( final FilterAction action: filterConfig.getFilters() ) {
			JButton button = new JButton();
			button.setText(action.getName());
			button.setIcon(action.getIcon());
			button.addActionListener( new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					previewPanel.setSelectedColTo(action);
				}
			});
			buttonMap.put(action.getId(), button);
			toolBar.add(button);
		}
		saveButton = new JButton(config.get("clients.files.tab.preview.filter.button.save.text"));
		saveButton.setIcon( new ImageIcon(config.get("clients.files.tab.preview.filter.button.save.icon")));
		saveButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO: Show dialog to save price column template  
				previewPanel.savePrices();
			}
		});

	}

}
